package directive;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modele.beans.Building;
import modele.beans.Schedule;
import modele.beans.Employee;
import modele.dao.BuildingDAO;
import modele.dao.EmployeeDAO;
import modele.dao.ScheduleDAO;
import modele.form.ScheduleForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class ScheduleAction extends GlobalAction {
	private EmployeeDAO empDAO;
	private BuildingDAO buildingDAO;
	private ScheduleDAO scheduleDAO;

	@Override
	public String executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		empDAO = new EmployeeDAO(this.conn);
		buildingDAO = new BuildingDAO(this.conn);
		scheduleDAO = new ScheduleDAO(this.conn);
		
		String type = (String) request.getParameter("type");
		String ac = ACTION_FAIL;

		if (type.equals("Ajouter"))
			ac = AddSchedule(mapping, form);
		else if (type.equals("Modifier"))
			ac = UpdateSchedule(mapping, form);
		else if (type.equals("Supprimer"))
			ac = DeleteSchedule(mapping, form);
		else
			ac = ACTION_SUCCESS;

		ScheduleForm scheduleForm = (ScheduleForm) form;
		
		if (ac.equals(ACTION_SUCCESS)) {
			scheduleForm.setBuildingID("");
			scheduleForm.setEmployeeID("");
			scheduleForm.setStartingDate("");
			scheduleForm.setEndingDate("");
			scheduleForm.setTask("");
		}

		scheduleForm.setSchedules(GetSchedules());
		scheduleForm.setEmployees(GetEmployees());
		scheduleForm.setBuildings(GetBuildings());

		request.setAttribute("ScheduleForm", scheduleForm);

		return ac;
	}

	private String AddSchedule(ActionMapping mapping, ActionForm form) {
		ScheduleForm schedule = (ScheduleForm) form;

		Schedule s = new Schedule(schedule.getStartingDate(), schedule.getEndingDate(), 
				new Employee("", "", "", "", schedule.getEmployeeID()), 
				new Building("", "", "", schedule.getBuildingID()), 
				schedule.getTask(), schedule.getId());
		try {
			scheduleDAO.AddSchedule(s);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}

	private String UpdateSchedule(ActionMapping mapping, ActionForm form) {
		ScheduleForm schedule = (ScheduleForm) form;

		Schedule s = new Schedule(schedule.getStartingDate(), schedule.getEndingDate(), 
				new Employee("", "", "", "", schedule.getEmployeeID()), 
				new Building("", "", "", schedule.getBuildingID()), 
				schedule.getTask(), schedule.getId());
		try {
			scheduleDAO.UpdateSchedule(s);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}

	private String DeleteSchedule(ActionMapping mapping, ActionForm form) {
		ScheduleForm schedule = (ScheduleForm) form;

		Schedule s = new Schedule(schedule.getStartingDate(), schedule.getEndingDate(), null, null, schedule.getTask(), schedule.getId());
		try {
			scheduleDAO.RemoveSchedule(s);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}

	private ArrayList<Schedule> GetSchedules() {
		try {
			return scheduleDAO.GetSchedules();
		} catch (SQLException e) {
			return new ArrayList<Schedule>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<Schedule>();
		}
	}

	private ArrayList<Employee> GetEmployees() {
		try {
			return empDAO.GetEmployees();
		} catch (SQLException e) {
			return new ArrayList<Employee>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<Employee>();
		}
	}

	private ArrayList<Building> GetBuildings() {
		try {
			return buildingDAO.GetBuildings();
		} catch (SQLException e) {
			return new ArrayList<Building>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<Building>();
		}
	}
}